 //
//  OrdinaryHomeVC.m
//  优学荟管理系统
//
//  Created by edz on 2017/11/18.
//  Copyright © 2017年 星道三好. All rights reserved.
//
#import "NewsDetailVC.h"
#import "SawScrollTiitModel.h"
#import "NewsDetailVC.h"
#import "TXScrollLabelView.h"
#import "OrdinaryHomeVC.h"
#import "HNCourseListVC.h"  // 课程列表
#import "OrdinaryTeacherAccountVC.h" // 老师
#import "ZYStudentDetailController.h" // 学生
#import "PackageListVC.h" // 套餐表表 \
#import "FundManagementVC.h" // 资金管理
#import "ZYMiyoshiBankController.h" // 三好银行
#import "ZYHistoricalRecordController.h" // 评价记录
#import "HNCommentMouldVC.h" // 三好评论模板
#import "ModifyPasswordVC.h" // 设置
#import "HNTeacherBusinessMoneyVC.h" // 业务员提成
#import "HNStrengtheningClassApplicationVC.h" // 加强课申请
#import "ZYNewsMessageController.h"
#import "ZYAttendanceRecordController.h"
#import "ZYTeachersAccountNumberController.h"
#import "ZYThreeGoodYoungPeopleController.h"
#import "XZStudentVC.h"
#import "HNTeacherManagerVC.h"
#import "FundManagementVC.h"
#import "XZNotificationList.h"


//#import "HNAssignmentVC.h"


#import "ZYHomeMemanagerController.h"

#import "HMNewClassListVC.h"


@interface OrdinaryHomeVC ()<TXScrollLabelViewDelegate>
{
    __weak IBOutlet UIView *NewsBackView;
    /** 机构图片 */
    __weak IBOutlet UIImageView *_Img;
    /** 机构名称 */
    __weak IBOutlet UILabel *_Name;
    /** 机构电话 */
    __weak IBOutlet UILabel *_Phone;
    /** 星币 */
    __weak IBOutlet UILabel *_Coins;
    /** 视图高度 */                              
    __weak IBOutlet NSLayoutConstraint *_ScrollViewHeight;
}
/** 按钮tag对应的名称 */
@property (nonatomic, strong) NSDictionary *Dict;
@property (nonatomic,strong) UIButton *customButton;
@property (nonatomic,strong) UILabel *redLabel;
@property(nonatomic,strong)SawScrollTiitModel *scrolModel;
@property(nonatomic,copy)NSString *url;
@end

@implementation OrdinaryHomeVC
-(SawScrollTiitModel *)scrolModel
{
    if (!_scrolModel) {
        _scrolModel = [[SawScrollTiitModel alloc]init];
    }
    return _scrolModel;
}
- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[LLTools createImageWithColor:ColorRGB(47, 204, 138)] forBarMetrics:(UIBarMetricsDefault)];

    
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [self setNetTowWork];
    
    [self.navigationController.navigationBar setBackgroundImage:[LLTools createImageWithColor:ColorRGB(184, 50, 59)] forBarMetrics:(UIBarMetricsDefault)];

}

- (void)viewDidLoad {
   // http://192.168.0.113:8080/StarShine/ws/trainLesson.do?action=countCommentReply
    [super viewDidLoad];
    // 动态计算
//    _ScrollViewHeight.constant *= KScale;
    // 消息
//    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTargat:self action:nil image:@"icon_message_default_new_normal" highImage:@"icon_message_default_new_normal"];
    
    [self setNetWork];
    self.customButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.customButton.frame = CGRectMake(0, 0, 60, 20);
    [self.customButton setImage:[UIImage imageNamed:@"icon_message_default_new_normal"] forState:(UIControlStateNormal)];
    
    
    self.customButton.hidden = YES;
    self.redLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, -2, 15, 15)];
    self.redLabel.backgroundColor = [UIColor whiteColor];
    self.redLabel.layer.cornerRadius = 7.5;
    self.redLabel.textColor = [UIColor redColor];
    self.redLabel.textAlignment = NSTextAlignmentCenter;
    self.redLabel.layer.borderColor = [UIColor redColor].CGColor;
    self.redLabel.layer.borderWidth = 1.5;
    self.redLabel.font = [UIFont systemFontOfSize:10];
    self.redLabel.hidden = YES;
    self.redLabel.layer.masksToBounds = YES;
    [self.customButton addSubview:self.redLabel];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.customButton];
    [self.customButton addTarget:self action:@selector(allButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    BOOL isPhoto = [NSString isBlankString:[AccountTool shareAccountTool].currentAccount.picture];
    
    if (isPhoto) {
         _Img.image =  [UIImage imageNamed:@"bg_home_head_default_new"];
    }else{
        [_Img sd_setImageWithURL:[NSURL URLWithString:[AccountTool shareAccountTool].currentAccount.picture] placeholderImage:[UIImage imageNamed:@"bg_home_head_default_new"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            
            _Img.layer.cornerRadius = _Img.width /2 ;
            _Img.layer.masksToBounds = YES;
        }];
    }
    
    
    _Name.text = [AccountTool shareAccountTool].currentAccount.websiteName;
    _Phone.text = [AccountTool shareAccountTool].currentAccount.username;
 
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(17, 17, 20 ,20)];
    [NewsBackView addSubview:img];
    img.image=[UIImage imageNamed:@"home_icon_news__"];
    
    __weak typeof(self) weakSelf = self;
    [self.scrolModel dataWithOrder:^(NSDictionary *dataDic) {
        
       __strong typeof(weakSelf) strongSelf = weakSelf;
        TXScrollLabelView *scrolLabelView = [TXScrollLabelView scrollWithTitle:dataDic[@"title"] type:TXScrollLabelViewTypeLeftRight velocity:1 options:UIViewAnimationOptionCurveEaseInOut];
        _url = dataDic[@"content"];
        scrolLabelView.frame = CGRectMake(CGRectGetMaxX(img.frame)+10, 12, kScreenWidth-32-17-20-20, 30);
        [NewsBackView addSubview:scrolLabelView];
        scrolLabelView.scrollLabelViewDelegate=strongSelf;
        scrolLabelView.scrollInset = UIEdgeInsetsMake(0, 10 , 0, 10);
        scrolLabelView.scrollSpace = 10;
        scrolLabelView.font = [UIFont systemFontOfSize:13];
        scrolLabelView.textAlignment = NSTextAlignmentCenter;
        scrolLabelView.scrollTitleColor=[UIColor blackColor];
        scrolLabelView.backgroundColor=[UIColor whiteColor];
        scrolLabelView.layer.cornerRadius = 5;
        
        
        [scrolLabelView beginScrolling];
    }];
   

}
-(void)scrollLabelView:(TXScrollLabelView *)scrollLabelView didClickWithText:(NSString *)text atIndex:(NSInteger)index
{
    NewsDetailVC *vc = [[NewsDetailVC alloc]init];
    vc.url = _url;
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)newsDetail
{
    NewsDetailVC *vc = [[NewsDetailVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)allButtonAction{
    
    ZYNewsMessageController *vc = [ZYNewsMessageController new];
    vc.popBalock = ^{
        
        [self setNetTowWork];
        [self setNetWork];
    };
    
    [self.navigationController pushViewController:vc animated:YES];
    
    
}


- (void)setNetTowWork{
    
    
    NSString *url = [NSString stringWithFormat:@"trainLesson.do?action=countCommentReply"];
    
    
    
    [NetManager Get:LLBaseURLs(url) Auth:YES Params:nil Success:^(id Obj) {
        
        if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
            
            NSString *t = [NSString stringWithFormat:@"%@",[Obj valueForKey:@"t"]];
            
            if ([t integerValue] == 0) {
                
                self.customButton.hidden = YES;
                self.redLabel.hidden = YES;
                
            }else{
                
                self.customButton.hidden = NO;
                self.redLabel.hidden = NO;
                self.redLabel.text = [NSString stringWithFormat:@"%@",t];
                
            }
            
            
            
        }
        
        
        
    } Fail:^(NSError *Error) {
        
    }];
    
    
    
    
}


- (void)setNetWork{
    
    NSString *url = [NSString stringWithFormat:@"user.do?action=getMyInfoAndRecharge"];
    
    NSLog(@"%@  %@",LLBaseURLs(url),[AccountTool shareAccountTool].currentAccount.authCode);
    
    
    [NetManager Get:LLBaseURLs(url) Auth:YES Params:nil Success:^(id Obj) {
        if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
            
            NSDictionary *t = [Obj valueForKey:@"t"];
            
           _Coins.text = [NSString stringWithFormat:@"%ld星币",[[t valueForKey:@"money"]  integerValue]];
           
        }
    } Fail:^(NSError *Error) {
        
    }];
    
    
    
    
    
    
}




#pragma mark - 事件
- (IBAction)Action:(UIButton *)sender {

    NSString *Key = self.Dict[[@(sender.tag) stringValue]];
    
    if ([Key isEqualToString:@"课程管理"]) {

        //新版课程列表
        HMNewClassListVC *VC = [HMNewClassListVC new];
        [self.navigationController pushViewController:VC animated:YES];
        
    }
    
    if ([Key isEqualToString:@"老师管理"]) {
        // 老师
        HNTeacherManagerVC *VC = [[HNTeacherManagerVC alloc] init];
        [self.navigationController pushViewController:VC animated:YES];
    }
    
    if ([Key isEqualToString:@"学生管理"]) {
        // 学生
        
//        ZYStudentDetailController *VC = [[ZYStudentDetailController alloc] init];
//        [self.navigationController pushViewController:VC animated:YES];
        XZStudentVC *VC = [[XZStudentVC alloc] init];
                [self.navigationController pushViewController:VC animated:YES];
    }
    
    if ([Key isEqualToString:@"作业管理"]) {
        // 三好小当家

//        ZYThreeGoodYoungPeopleController *VC = [[ZYThreeGoodYoungPeopleController alloc] init];
//        [self.navigationController pushViewController:VC animated:YES];
        [self.navigationController pushViewController:[ZYHomeMemanagerController new] animated:YES];
    }
    
    if ([Key isEqualToString:@"资金管理"]) {
        // 资金管理
        FundManagementVC *VC = [[FundManagementVC alloc] init];
        [self.navigationController pushViewController:VC animated:YES];
    }
    
    if ([Key isEqualToString:@"三好银行"]) {
        // 三好银行
        ZYMiyoshiBankController *VC = [[ZYMiyoshiBankController alloc] init];
        [self.navigationController pushViewController:VC animated:YES];
    }
    
    if ([Key isEqualToString:@"评价记录"]) {
        // 评价记录
        ZYHistoricalRecordController *VC = [[ZYHistoricalRecordController alloc] init];
        [self.navigationController pushViewController:VC animated:YES];
    }
    
    if ([Key isEqualToString:@"三好评价模板"]) {
        // 三好评价模板
        HNCommentMouldVC *VC = [[HNCommentMouldVC alloc] init];
        [self.navigationController pushViewController:VC animated:YES];
    }
    
    if ([Key isEqualToString:@"设置"]) {
        // 设置
        ModifyPasswordVC *VC = [[ModifyPasswordVC alloc] init];
        [self.navigationController pushViewController:VC animated:YES];
    }

    
    if ([Key isEqualToString:@"加强课"]) {
        // 加强课申请
        HNStrengtheningClassApplicationVC *VC = [[HNStrengtheningClassApplicationVC alloc] init];
        [self.navigationController pushViewController:VC animated:YES];
    }

    if ([Key isEqualToString:@"套餐列表"]) {
        PackageListVC *VC = [[PackageListVC alloc] init];
        [self.navigationController pushViewController:VC animated:YES];
    }
    
//    if ([Key isEqualToString:@"作业批注"]) {
//
//        [self.navigationController pushViewController:[ZYHomeMemanagerController new] animated:YES];
//
//
//    }
    
    if ([Key isEqualToString:@"机构通告"]) {
        XZNotificationList *VC = [[XZNotificationList alloc] init];
        [self.navigationController pushViewController:VC animated:YES];
    }
    
}



#pragma mark - 方法

#pragma mark - Get
- (NSDictionary *)Dict {
    
    if (!_Dict) {
        
        _Dict = @{@"1100":@"课程管理", @"1101":@"老师管理", @"1102":@"学生管理",
                  @"1103":@"作业管理", @"1104":@"资金管理",
                  @"2100":@"三好银行", @"2101":@"评价记录",
                  @"2104":@"机构通告", @"2102":@"三好评价模板",@"2108":@"机构反馈",@"2109":@"反馈列表",
                  @"2103":@"设置", @"2105":@"加强课"
                  ,@"2107":@"套餐列表"};
    }
    return _Dict;
}
@end
